package com.gljx.web.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.TimingChargeEntry;
import com.gljx.web.Entity.TrainCharge;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.dao.TimingChargeEntryMapper;
import com.gljx.web.dao.TimingChargeMapper;
import com.gljx.web.dao.TrainChargeMapper;
import com.gljx.web.service.TimingChargeService;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class TimingChargeServiceImpl extends CommonDao implements TimingChargeService{
	
	@Autowired
	private TimingChargeMapper timingChargeMapper;
	
	
	@Autowired
	private TimingChargeEntryMapper timingChargeEntryMapper;
	
	
	@Autowired
	private StudentMapper studentMapper;
	
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.TimingChargeMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	  取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.TimingChargeMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	
	
	/**
	 * 添加计时收费单据
	 */
	@Override
	public void addTimingCharge(TrainCharge trainCharge, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfsex, String[] cfidcardno, String[] cfidcardaddress, String[] cftjr, String[] cfclazz,
			String[] cfclazzamount, String[] cfyishou, String[] cfysje, String[] cfguifei, String[] cfpxf,
			String[] cfcash, String[] cfcard, String[] cfguazhang, String[] trainObject, String[] timingStartTime,
			String[] timingEndTime, String[] cfyhzz, String[] cfshje, String[] cfyhje, String[] cfyhj, String[] cfqfje,
			String[] cfremark,String[] cfsubject,String[] cfcoachname,String[] cfbegintime,String[] cfendtime,String[] cfstudentname) {
		//添加父类
				timingChargeMapper.insertSelective(trainCharge);
				//添加子类
				for (int i = 0; i < fseq.length; i++) {
					TimingChargeEntry timingChargeEntry = new TimingChargeEntry();
					timingChargeEntry.setCfstudentno(cfstudentno[i]);
					timingChargeEntry.setCfstudentid(cfstudentid[i]);
					if("".equals(cfsex[i]) || cfsex.length == 0){

					}else{
						timingChargeEntry.setCfsex(Long.parseLong(cfsex[i]));
					}
					timingChargeEntry.setCfidcardno(cfidcardno[i]);
					timingChargeEntry.setCfidcardaddress(cfidcardaddress[i]);
					timingChargeEntry.setCftjr(cftjr[i]);
					timingChargeEntry.setCfclazz(cfclazz[i]);
					if("".equals(cfclazzamount[i])){

					}else{
						timingChargeEntry.setCfclazzamount(BigDecimalFormat.formot(cfclazzamount[i]));
					}
					if("".equals(cfyishou[i])){

					}else{
						timingChargeEntry.setCfyishou(BigDecimalFormat.formot(cfyishou[i]));
					}
					if("".equals(cfysje[i])){

					}else{
						timingChargeEntry.setCfysje(BigDecimalFormat.formot(cfysje[i]));
					}
					if("".equals(cfguifei[i])){

					}else{
						timingChargeEntry.setCfguifei(BigDecimalFormat.formot(cfguifei[i]));
					}
					if("".equals(cfpxf[i])){

					}else{
						timingChargeEntry.setCfpxf(BigDecimalFormat.formot(cfpxf[i]));
					}
					if("".equals(cfcash[i])){

					}else{
						timingChargeEntry.setCfcash(BigDecimalFormat.formot(cfcash[i]));
					}
					if("".equals(cfcard[i])){

					}else{
						timingChargeEntry.setCfcard(BigDecimalFormat.formot(cfcard[i]));
					}
					if("".equals(cfguazhang[i])){

					}else{
						timingChargeEntry.setCfguazhang(BigDecimalFormat.formot(cfguazhang[i]));
					}
					if("".equals(cfyhzz[i])){

					}else{
						timingChargeEntry.setCfyhzz(BigDecimalFormat.formot(cfyhzz[i]));
					}
					if("".equals(cfshje[i])){

					}else{
						timingChargeEntry.setCfshje(BigDecimalFormat.formot(cfshje[i]));
					}
					if("".equals(cfyhje[i])){

					}else{
						timingChargeEntry.setCfyhje(BigDecimalFormat.formot(cfyhje[i]));
					}
					if("".equals(cfyhj[i])){

					}else{
						timingChargeEntry.setCfyhj(BigDecimalFormat.formot(cfyhj[i]));
					}
					if("".equals(cfqfje[i])){

					}else{
						timingChargeEntry.setCfqfje(BigDecimalFormat.formot(cfqfje[i]));
					}
					if("".equals(cfsubject[i])){

					}else{
						if(cfsubject[i].equals("科目二")) {
							timingChargeEntry.setCfsubject(2);
						}
						if(cfsubject[i].equals("科目三")) {
							timingChargeEntry.setCfsubject(3);
						}
						
					}
					if("".equals(cfcoachname[i])){

					}else{
						timingChargeEntry.setCfcoachname(cfcoachname[i]);
					}
					if("".equals(cfbegintime[i])){

					}else{
						timingChargeEntry.setCfbegintime(cfbegintime[i]);
					}
					if("".equals(cfendtime[i])){

					}else{
						timingChargeEntry.setCfendtime(cfendtime[i]);
					}
					if("".equals(cfstudentname[i])){

					}else{
						timingChargeEntry.setStudentName(cfstudentname[i]);
					}
					timingChargeEntry.setFparentid(trainCharge.getFid());
					timingChargeEntry.setCfremark(cfremark[i]);
					timingChargeEntry.setFid(GetFid.UUIDIntoFid());
					timingChargeEntry.setFseq((long) (i + 1));
					//添加子类
					timingChargeEntryMapper.insertSelective (timingChargeEntry);
				}
	}

	@Override
	public TrainCharge queryByTrainChargeFid(String fid) {
		return timingChargeMapper.selectByPrimaryKey(fid);
	}

	/**
	 * 修改计时收费
	 */
	@Override
	public void modifyTimingCharge(TrainCharge trainCharge, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfsex, String[] cfidcardno, String[] cfidcardaddress, String[] cftjr, String[] cfclazz,
			String[] cfclazzamount, String[] cfyishou, String[] cfysje, String[] cfguifei, String[] cfpxf,
			String[] cfcash, String[] cfcard, String[] cfguazhang, String[] cfyhzz, String[] cfshje, String[] cfyhje,
			String[] cfyhj, String[] cfqfje, String[] cfremark,String cfsubject[],String cfcoachname[], String cfbegintime[],String cfendtime[],String cfstudentname[]) {
		//修改父类
		timingChargeMapper.updateByPrimaryKeySelective(trainCharge);
		//根据父类id删除
		timingChargeEntryMapper.deleteByFparentid(trainCharge.getFid());
		//添加子类
		if(fseq == null || fseq.length == 0){
			
		}else {
			for (int i = 0; i < fseq.length; i++) {
				TimingChargeEntry timingChargeEntry = new TimingChargeEntry();
				timingChargeEntry.setCfstudentno(cfstudentno[i]);
				timingChargeEntry.setCfstudentid(cfstudentid[i]);
				if("".equals(cfsex[i]) || cfsex.length == 0){
	
				}else{
					timingChargeEntry.setCfsex(Long.parseLong(cfsex[i]));
				}
				timingChargeEntry.setCfidcardno(cfidcardno[i]);
				timingChargeEntry.setCfidcardaddress(cfidcardaddress[i]);
				timingChargeEntry.setCftjr(cftjr[i]);
				timingChargeEntry.setCfclazz(cfclazz[i]);
				if("".equals(cfclazzamount[i])){
	
				}else{
					timingChargeEntry.setCfclazzamount(BigDecimalFormat.formot(cfclazzamount[i]));
				}
				if("".equals(cfyishou[i])){
	
				}else{
					timingChargeEntry.setCfyishou(BigDecimalFormat.formot(cfyishou[i]));
				}
				if("".equals(cfysje[i])){
	
				}else{
					timingChargeEntry.setCfysje(BigDecimalFormat.formot(cfysje[i]));
				}
				if("".equals(cfguifei[i])){
	
				}else{
					timingChargeEntry.setCfguifei(BigDecimalFormat.formot(cfguifei[i]));
				}
				if("".equals(cfpxf[i])){
	
				}else{
					timingChargeEntry.setCfpxf(BigDecimalFormat.formot(cfpxf[i]));
				}
				if("".equals(cfcash[i])){
	
				}else{
					timingChargeEntry.setCfcash(BigDecimalFormat.formot(cfcash[i]));
				}
				if("".equals(cfcard[i])){
	
				}else{
					timingChargeEntry.setCfcard(BigDecimalFormat.formot(cfcard[i]));
				}
				if("".equals(cfguazhang[i])){
	
				}else{
					timingChargeEntry.setCfguazhang(BigDecimalFormat.formot(cfguazhang[i]));
				}
				if("".equals(cfyhzz[i])){
	
				}else{
					timingChargeEntry.setCfyhzz(BigDecimalFormat.formot(cfyhzz[i]));
				}
				if("".equals(cfshje[i])){
	
				}else{
					timingChargeEntry.setCfshje(BigDecimalFormat.formot(cfshje[i]));
				}
				if("".equals(cfyhje[i])){
	
				}else{
					timingChargeEntry.setCfyhje(BigDecimalFormat.formot(cfyhje[i]));
				}
				if("".equals(cfyhj[i])){
	
				}else{
					timingChargeEntry.setCfyhj(BigDecimalFormat.formot(cfyhj[i]));
				}
				if("".equals(cfqfje[i])){
	
				}else{
					timingChargeEntry.setCfqfje(BigDecimalFormat.formot(cfqfje[i]));
				}
				if("".equals(cfsubject[i])){

				}else{
					if(cfsubject[i].equals("科目二")) {
						timingChargeEntry.setCfsubject(2);
					}
					if(cfsubject[i].equals("科目三")) {
						timingChargeEntry.setCfsubject(3);
					}
					
				}
				if("".equals(cfcoachname[i])){

				}else{
					timingChargeEntry.setCfcoachname(cfcoachname[i]);
				}
				if("".equals(cfbegintime[i])){

				}else{
					timingChargeEntry.setCfbegintime(cfbegintime[i]);
				}
				if("".equals(cfendtime[i])){

				}else{
					timingChargeEntry.setCfendtime(cfendtime[i]);
				}
				if("".equals(cfstudentname[i])){

				}else{
					timingChargeEntry.setStudentName(cfstudentname[i]);
				}
				timingChargeEntry.setFparentid(trainCharge.getFid());
				timingChargeEntry.setCfremark(cfremark[i]);
				timingChargeEntry.setFid(GetFid.UUIDIntoFid());
				timingChargeEntry.setFseq((long) (i + 1));
				//添加子类
				timingChargeEntryMapper.insertSelective(timingChargeEntry);

			}
		}
	}

	/**
	 * 审核
	 */
	@Override
	public void auditTrainCharge(List<TimingChargeEntry> timingChargeEntries, TrainCharge trainCharge) {
		/**
		 * 修改学生
		 */
		for (TimingChargeEntry timingChargeEntry : timingChargeEntries) {
			Student student = new Student();
			student.setFid(timingChargeEntry.getCfstudentid());
			//获取优惠金额
			BigDecimal cfyhje = timingChargeEntry.getCfyhje();
			//已收金额
			student.setCfreceived(timingChargeEntry.getCfshje());
			//欠费金额
			student.setCfarrearage(timingChargeEntry.getCfqfje());
			if (cfyhje == null || "".equals(cfyhje)) {
				
			}else{
				student.setCffavorable(cfyhje);
			}
			studentMapper.updateByPrimaryKeySelective(student);
		}
		//修改父类
		timingChargeMapper.updateByPrimaryKeySelective(trainCharge);
	}

	
	
	@Override
	public void updateTimingCharge(TrainCharge trainCharge) {
		timingChargeMapper.updateByPrimaryKeySelective(trainCharge);
	}

	@Override
	public TrainCharge selectSystemParamByName(String paramId) {
		return timingChargeMapper.selectSystemParamByName(paramId);
	}
	
	
	
}
